package com.hxkj.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.CompanyinfoDao;
import com.hxkj.domain.enums.CompanyInfoExceptionEnum;
import com.hxkj.domain.enums.CompanyInfoSuccessEnum;
import com.hxkj.domain.po.Companyinfo;
import com.hxkj.service.CompanyInfoService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 合作公司实现类
 * <p>
 * Created by yaosy on 2018年6月8日13:58:01
 */
@Service
@Slf4j
public class CompanyInfoServiceImpl implements CompanyInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CompanyInfoServiceImpl.class);

    @Autowired
    private CompanyinfoDao companyinfoDao;


    @Override
    public PageInfo getList(Companyinfo companyinfo, PageTo pageParam) {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        Page<Companyinfo> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        PageInfo<Companyinfo> pageInfo = null;
        List<Companyinfo> companyinfoList = new ArrayList<>();
       try{
           pageInfo = new PageInfo<>(page);
           companyinfoList = companyinfoDao.get(companyinfo);
           List<Companyinfo> companyinfoList1 = new ArrayList<>();
           for(Companyinfo company : companyinfoList){
                Companyinfo c = new Companyinfo();
                c.setCompanyId(company.getCompanyId());
                c.setCompanyName(company.getCompanyName());
                c.setCompanyLogo(company.getCompanyLogo());
                c.setOnlineTime(company.getOnlineTime());
                c.setRegisteredAddress(company.getRegisteredAddress());
                c.setRegisteredCapital(company.getRegisteredCapital());
                c.setStatus(company.getStatus());
                c.setSortNumber(company.getSortNumber());
                c.setIncomeRange(company.getIncomeRange());
                companyinfoList1.add(c);
           }
           pageInfo.setTotal(companyinfoList1.size());
           pageInfo.setList(companyinfoList1);
       }catch(Exception e){
           e.printStackTrace();
       }
       return pageInfo;
    }

    @Override
    public ResultDataStruct<Companyinfo> get(Companyinfo companyinfo) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        try{
            Long companyId = companyinfo.getCompanyId();
            Companyinfo companyinfo1 =  companyinfoDao.get(companyId);
            resultDataStruct.setData(companyinfo1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultDataStruct;
    }

    @Override
    public  ResultDataStruct<Companyinfo> save(Companyinfo companyinfo) throws Exception{
        ResultDataStruct<Companyinfo>  result = new ResultDataStruct();
        try{
            //判断公司名称是否存在
            Companyinfo company = new Companyinfo();
            company.setCompanyName(companyinfo.getCompanyName());
            company.setDelTag(0);
            List<Companyinfo> companyinfoList = companyinfoDao.get(company);
            if(companyinfoList != null && companyinfoList.size() > 0){
                result.setMessage(CompanyInfoExceptionEnum.COMPANY_INFO_EXISTS_FAILED.getMsg());
                result.setCode(CompanyInfoExceptionEnum.COMPANY_INFO_EXISTS_FAILED.getCode());
                return result;
            }
            companyinfoDao.save(companyinfo);
        }catch(Exception e){
            throw e;
        }
        result.setData(companyinfo);
        return result;
    }

    @Override
    public ResultMessageStruct modify(Companyinfo companyinfo) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            Long companyId = companyinfo.getCompanyId();
            if(companyId==null){
                resultMessageStruct.setCode(CompanyInfoExceptionEnum.COMPANY_INFO_EXCEPTION_NOT_ID.getCode());
                resultMessageStruct.setMessage(CompanyInfoExceptionEnum.COMPANY_INFO_EXCEPTION_NOT_ID.getMsg());
                return resultMessageStruct;
            }
            Companyinfo info = new Companyinfo();
            info.setCompanyId(companyId);
            info.setCompanyName(companyinfo.getCompanyName());
            info.setDelTag(0);
            int count =  companyinfoDao.getCompany(info);
            if(count > 0){
                resultMessageStruct.setMessage(CompanyInfoExceptionEnum.COMPANY_INFO_EXISTS_FAILED.getMsg());
                resultMessageStruct.setCode(CompanyInfoExceptionEnum.COMPANY_INFO_EXISTS_FAILED.getCode());
                return resultMessageStruct;
            }
            Companyinfo companyinfo1 =  companyinfoDao.get(companyId);
            if(companyinfo1 != null) {
                companyinfo1.setBiref(companyinfo.getBiref());
                companyinfo1.setBrokersAgreement(companyinfo.getBrokersAgreement());
                companyinfo1.setCompanyCode(companyinfo.getCompanyCode());
                companyinfo1.setCompanyName(companyinfo.getCompanyName());
                companyinfo1.setCompanyLogo(companyinfo.getCompanyLogo());
                companyinfo1.setCompanyDetails(companyinfo.getCompanyDetails());
                companyinfo1.setOnlineTime(companyinfo.getOnlineTime());
                companyinfo1.setRegisteredAddress(companyinfo.getRegisteredAddress());
                companyinfo1.setRegisteredCapital(companyinfo.getRegisteredCapital());
                companyinfo1.setCompanyWebsite(companyinfo.getCompanyWebsite());
                companyinfo1.setCompanySoftware(companyinfo.getCompanySoftware());
                companyinfo1.setIncomeRange(companyinfo.getIncomeRange());
                companyinfo1.setCompanyPhone(companyinfo.getCompanyPhone());
                companyinfo1.setCompanyPerson(companyinfo.getCompanyPerson());
                companyinfo1.setCompanyContactsPerson(companyinfo.getCompanyContactsPerson());
                companyinfo1.setIcpRecord(companyinfo.getIcpRecord());
                companyinfo1.setBrokersAgreement(companyinfo.getBrokersAgreement());
                companyinfo1.setRightAction(companyinfo.getRightAction());
                companyinfo1.setCompanyProfile(companyinfo.getCompanyProfile());
                companyinfo1.setSortNumber(companyinfo.getSortNumber());
                companyinfo1.setUpdateTime(companyinfo.getUpdateTime());
                companyinfo1.setModifier(companyinfo.getModifier());
                companyinfo1.setStatus(companyinfo.getStatus());
                companyinfoDao.update(companyinfo1);
            }
        }catch(Exception e){
            e.printStackTrace();
            resultMessageStruct.setCode(CompanyInfoExceptionEnum.COMPANY_INFO_EDIT_FAILED.getCode());
            resultMessageStruct.setMessage(CompanyInfoExceptionEnum.COMPANY_INFO_EDIT_FAILED.getMsg());
        }
        return resultMessageStruct;
    }

    @Override
    public ResultMessageStruct remove(Companyinfo companyinfo) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            if (companyinfo.getCompanyIds() == null) {
                resultMessageStruct.setCode(CompanyInfoExceptionEnum.COMPANY_INFO_EXCEPTION_NOT_ID.getCode());
                return resultMessageStruct;
            }
            String[] companyIds = companyinfo.getCompanyIds().split(",");
            for (int i = 0; i < companyIds.length; i++) {
                companyinfo.setDelTag(1);
                companyinfo.setUpdateTime(System.currentTimeMillis());
                companyinfo.setCompanyId(Long.valueOf(companyIds[i]+""));
                companyinfoDao.update(companyinfo);
            }
        }catch(Exception e){
            e.printStackTrace();
            resultMessageStruct.setCode(CompanyInfoExceptionEnum.COMPANY_INFO_DELETE_FAILED.getCode());
            resultMessageStruct.setMessage(CompanyInfoExceptionEnum.COMPANY_INFO_DELETE_FAILED.getMsg());
        }
        return resultMessageStruct;
    }

    @Override
    public ResultMessageStruct sort(Companyinfo companyinfo) {
        ResultMessageStruct result = new ResultMessageStruct();
        try{
            //解析批量操作的功能id
            if (companyinfo.getCompanyIds() == null) {
                result.setCode(CompanyInfoExceptionEnum.COMPANY_INFO_EXCEPTION_NOT_ID.getCode());
                return result;
            }
            String[] companyIds = companyinfo.getCompanyIds().split(",");
            Companyinfo companyinfo1 = new Companyinfo();
            for (int i = 0; i < companyIds.length; i++) {
                companyinfo1 = new Companyinfo();
                companyinfo1.setCompanyId(Long.valueOf(companyIds[i]));
                companyinfo1.setSortNumber((long) i);
                companyinfoDao.update(companyinfo1);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        result.setCode(CompanyInfoSuccessEnum.COMPANY_INFO_SUCCESS_ENUM.getCode());
        result.setMessage(CompanyInfoSuccessEnum.COMPANY_INFO_SUCCESS_ENUM.getMsg());
        return result;
    }

}
